/*
 * name: mabel xu
 * date: sept 18, 2007
 * filename: ManageTeam.java
 * description: manages a team of HSAthletes
 */

import java.util.*;
import java.io.*;
import java.lang.Math;

public class ManageTeam
{
	ArrayList<HSAthlete> athletes;

	// constructor that initializes ArrayList athletes to blank ArrayList<HSAthlete>
	public ManageTeam()
	{
		athletes = new ArrayList<HSAthlete>();
	}		// end constructor

	// pre: ManageTeam object has been created, athletes ArrayList initialized
	// post: finishes program if user enters 9
	public void displayMenu()
	{
		int choice = 0;

		while (choice != 9)
		{
			System.out.println("Welcome to Mabel's HSAthletes Program");
			System.out.println();
			System.out.println("1. display all athletes");
			System.out.println("2. enter athletes from file");
			System.out.println("3. enter single athlete");
			System.out.println("4. edit info for athlete");
			System.out.println("5. calculate average gpa");
			System.out.println("6. count number per grade");
			System.out.println("7. find athlete");
			System.out.println("8. remove athlete");
			System.out.println("9. exit");
			System.out.println();

			Scanner in = new Scanner(System.in);
			System.out.print("please pick a menu choice: ");
			try
			{
				choice = in.nextInt();
			}		// end of try to get menu choice
			catch (Exception e)
			{
				System.out.println("sorry, please enter # from 1 to 9");
				System.out.println();
				break;
			}		// end of exception error for menu choice

			switch(choice)
			{
				case 1: display(); break;
				case 2: enterMultiple(); break;
				case 3: enterSingle(); break;
				case 4: editInformation(); break;
				case 5: calculateGPA(); break;
				case 6: countPerGrade(); break;
				case 7: findAthlete(); break;
				case 8: removeAthlete(); break;
				case 9: break;
				default:
					System.out.println("sorry, please enter # from 1 to 9");
					System.out.println();
					break;
			};		// end of switch that calls appropriate method depending on menu choice

			System.out.println();
		}		// end of while loop (keep going until user picks "exit")
	}		// end of displayMenu() method

	// pre: ArrayList athletes exists (does not need elements)
	// post: displays all athletes or says "no athletes in team"
	private void display()
	{
		if (athletes.size() == 0)
		{
			System.out.println("oops, there are no athletes on this team -- please add some!");
		}		// end of if there's no athletes in ArrayList
		else
		{
			for(HSAthlete a : athletes)
			{
				System.out.println(a.toString());
			}		// end fo for (display each HSAthlete in ArrayList)
		}		// end of else (ah ... there are athletes in the ArrayList)

		System.out.println();
	}		// end of display() method (displays all athletes in ArrayList)

	// pre: ArrayList athletes initialized (doesn't matter # of elements)
	// post: enters athletes from text file and displays each athlete as it's added
	private void enterMultiple()
	{
		Scanner in = new Scanner(System.in);
		System.out.print("please enter a file name: ");
		String fileName = in.next();

		Scanner file;
		try
		{
			file = new Scanner(new File(fileName));

			try
			{
				while(file.hasNext())
				{
					String f = file.next();
					String l = file.next();
					int gr = file.nextInt();
					boolean p = file.nextBoolean();
					double g = file.nextDouble();

					athletes.add(new HSAthlete(f, l, gr, p, g));
					System.out.println(athletes.get(athletes.size() - 1).toString());
				}		// end of while (keeps reading file until data all gone)
			}		// end if try (tries to read file)
			catch (Exception e)
			{
				System.out.println("sorry, there's something wrong with this file");
			}		// end of exception (oops, file found but has error)
		}		// end of try (tries to open file)
		catch (Exception e)
		{
			System.out.println("sorry, invalid file name");
		}		// end of exception (oops, file not found)

		System.out.println();
	}		// end of enterMultiple() (reads athletes from a file)

	// pre: ArrayList athletes initialized (can have 0 elements)
	// post: adds one new athlete (using user input) to end of ArrayList
	private void enterSingle()
	{
		Scanner in = new Scanner(System.in);

		System.out.print("first name? ");
		String f = in.next();
		System.out.print("last name? ");
		String l = in.next();
		System.out.print("grade lvl? ");
		int gr = in.nextInt();
		System.out.print("physical? ");
		boolean p = in.nextBoolean();
		System.out.print("gpa? ");
		double g = in.nextDouble();

		athletes.add(new HSAthlete(f, l, gr, p, g));

		System.out.println();
	}		// end of enterSingle() (adds athlete based on user info - one at a time)

	// pre: athletes initialized (doesn't have to have any elements)
	// post: edits athlete if found, says "athlete not found" otherwise
	private void editInformation()
	{
		Scanner in = new Scanner(System.in);

		System.out.print("last name? ");
		String l = in.next();

		int index = -1;
		for(int i = 0; i < athletes.size() && index < 0; i++)
		{
			if(athletes.get(i).getLastName().equals(l))
				index = i;
		}	// end for loop (goes through and tries to find athlete)

		if (index < 0)
		{
			System.out.println("Sorry, athlete not found");
		}		// end of if (in case athlete not found)
		else
		{
			System.out.println("Athlete found: ");
			System.out.println(athletes.get(index).toString());
			System.out.println();
			System.out.print("first name? ");
			String f = in.next();
			System.out.print("grade lvl? ");
			int gr = in.nextInt();
			System.out.print("physical? ");
			boolean p = in.nextBoolean();
			System.out.print("gpa? ");
			double g = in.nextDouble();

			athletes.set(index, new HSAthlete(f, l, gr, p, g));
			System.out.println("new info: ");
			System.out.println(athletes.get(index).toString());
		}		// end of else (where athlete is found, and information is edited)

		System.out.println();
	}		// end of editInformation() (edits information for single athlete)

	// pre: athletes initialized
	// post: adds all gpas from athletes ArrayList and displays average (2 decimals)
	private void calculateGPA()
	{
		double sum = 0.00;
		for(HSAthlete a : athletes)
		{
			sum += a.getGPA();
		}		// end of for loop (loops through and sums all gpas)

		System.out.println("average GPA for team: " + Math.floor(sum / athletes.size()));
		System.out.println();
	}		// end of calculateGPA() (calculates and displays average gpa for all athletes)

	// pre: athletes initialized
	// post: counts and displays # of athletes in 9, 10, 11 and 12 grade
	private void countPerGrade()
	{
		int freshmen = 0;
		int sophomores = 0;
		int juniors = 0;
		int seniors = 0;

		for(HSAthlete a : athletes)
		{
			switch (a.getGrade())
			{
				case  9: freshmen++; break;
				case 10: sophomores++; break;
				case 11: juniors++; break;
				case 12: seniors++; break;
			}		// end of switch case (increments appropriate counter depending on grade lvl)
		}		// end of for loop (goes through each athlete ... and reads grade lvl)

		System.out.println("# of freshmen - " + freshmen);
		System.out.println("# of sophomores - " + sophomores);
		System.out.println("# of juniors - " + juniors);
		System.out.println("# of seniors - " + seniors);
		System.out.println();
	}		// end of countPerGrade() (counts and displays # of athletes in 9, 10, 11, and 12th grade)

	// pre: athletes initialized
	// post: displays appropriate message depending on athlete found or not (based on user input)
	private void findAthlete()
	{
		Scanner in = new Scanner(System.in);
		System.out.print("first name? ");
		String f = in.next();
		System.out.print("last name? ");
		String l = in.next();
		System.out.print("grade lvl? ");
		int gr = in.nextInt();
		System.out.print("physical? ");
		boolean p = in.nextBoolean();
		System.out.print("gpa? ");
		double gpa = in.nextDouble();
		System.out.println();

		boolean found = false;
		for(HSAthlete a : athletes)
		{
			if (a.equals(new HSAthlete(f, l, gr, p, gpa)))
			{
				found = true;
				break;
			}		// end if (in case athlete is found)
		}		// end of for loop (keep looking until athlete found)

		System.out.println("Athlete " + (found ? "" : "NOT ") + "found");
		System.out.println();
	}		// end of fineAthlete() (trys to find athlete, tells user if athlete found)

	// pre: athletes initialized
	// post: removes all athletes that match given first and last name
	private void removeAthlete()
	{
		Scanner in = new Scanner(System.in);
		System.out.print("first name? ");
		String f = in.next();
		System.out.print("last name? ");
		String l = in.next();

		int removed = 0;
		for(int i = athletes.size() - 1; i >= 0; i--)
		{
			if (athletes.get(i).getFirstName().equals(f) && athletes.get(i).getLastName().equals(l))
			{
				athletes.remove(i);
				removed++;
			}		// end of if (remove athlete if athlete matches user input)
		}		// end of for loop (loops through backwards and removes all athletes)

		System.out.println(removed == 0 ?
								"no athletes removed" :
								removed + (removed == 1 ? " athlete " : " athletes ") + "removed");
		System.out.println();
	}		// end of removeAthlete() (removes all athletes with matching first and last name)

	// pre: nothing at all!
	// post: creates ManageTeam and starts the program (for testing purposes)
	public static void main(String[] args)
	{
		ManageTeam mt = new ManageTeam();
		mt.displayMenu();
	}		// end of main() (used for testing purposes)
}		// end of ManageTeam() class